package common.fetch.custom;

import com.han.http.client.paramter.RequestHeader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Objects;

/**
 * @author: Han
 * @date: 2018/8/21 16:11
 * @dscription: 通过指定编码抓取数据
 */
public class FetcherByCharset {
    public static String getContent(String urlStr, RequestHeader[] requestHeaders) throws IOException {
        StringBuilder page = new StringBuilder();
        BufferedReader reader = null;
        HttpURLConnection urlConnection = null;
        try {
            URL url = new URL(urlStr);
            urlConnection = (HttpURLConnection) url.openConnection();

            for (RequestHeader header : requestHeaders) {
                String key = header.getKey();
                String value = header.getValue();
                urlConnection.setRequestProperty(key, value);
            }
            reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "gb2312"));
            String line;
            while ((line = reader.readLine()) != null) {
                page.append(line).append("\n");
            }
        } finally {
            try {
                Objects.requireNonNull(reader).close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Objects.requireNonNull(urlConnection).disconnect();
        }
        return page.toString();
    }
}
